package com.miplus.ccrm.admin.controller;

import com.miplus.ccrm.admin.controller.vo.*;
import com.miplus.ccrm.admin.pojo.SysUser;
import com.miplus.ccrm.admin.security.JwtToken;
import com.miplus.ccrm.admin.service.*;
import com.miplus.ccrm.admin.utils.SpringContextUtil;
import com.miplus.ccrm.common.*;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 1-2.系统管理服务接口
 */
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/sys")
public class SysController {
    //GET接口加@RequestParam与不加@RequestParam区别，加了表示输入参数要一一对应，否则出错，没加，输入参数可以少
    @Resource
    private SysGrpService sysGrpService;

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysNamesService sysNamesService;

    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private SysPermissionService sysPermissionService;

    @Resource
    private SysAuditLogService sysAuditLogService;

    @Resource
    private SysTagsService sysTagsService;

    @Resource
    private JDataService dataService;

    /**
     * 取对应组信息Tree
     */
    @GetMapping("/grpTree")
    public R<List<GrpTreeResVo>> getGrpTree(@RequestParam Map<String, Object> params) {
        String usrid= SpringContextUtil.getCurrUsrID(params);
        return R.Success(sysGrpService.getGrpTree(usrid,"",""));
    }

    /**
     * 取有效组Tree
     */
    @GetMapping("/getActiveGrp")
    public R<List<GrpTreeResVo>> getActiveGrp(String pid,String gtype) {
        return R.Success(sysGrpService.getGrpTree("",pid,gtype));
    }

    /**
     * 取当前用户有权限的组
     * 参数例子：gtype=1(内催部门)，=5(委外机构)，空是全部
     */
    @GetMapping("/getPermGrpList")
    public R<List<NamesResVo>> getPermGrpList(String gtype) {
        String usrid= SpringContextUtil.getCurrUsrID();
        return R.Success(sysGrpService.getPermGrpList(usrid,gtype));
    }

    /**
     * 取有效的组
     * 参数例子：gtype=1(内催部门)，=5(委外机构)，空是全部
     */
    @GetMapping("/getActiveGrpList")
    public R<List<NamesResVo>> getActiveGrpList(String gtype) {
        return R.Success(sysGrpService.getActiveGrpList(gtype));
    }

    /**
     * 取用户信息及权限List
     */
    @GetMapping("/systemUserSelect")
    public R<PageData<UserInfoResVo>> systemUserSelect(@Validated UserInfoReqVo userInfoReqVo) {
        String usrid= SpringContextUtil.getCurrUsrID();
        return R.SuccessPage(sysUserService.systemUserSelect(userInfoReqVo,usrid));
    }

    /**
     * 取角色值及名称LIST
     */
    @GetMapping("/roleSelectList")
    public R<List<NamesResVo>> roleSelectList() {
        String usrid= SpringContextUtil.getCurrUsrID();
        return R.Success(sysRoleService.roleSelectList(usrid));
    }

    /**
     * 获取角色关联的组
     */
    @GetMapping("/roleGroup")
    public R<List<String>> roleGroup(@RequestParam @NotNull String roleid) {
        return R.Success(sysRoleService.roleGroup(roleid));
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/userInfo")
    public R<UserInfoResVo> userInfo(@RequestParam @NotNull String usrid) {
        UserInfoResVo resVo=sysUserService.quyUserInfo(usrid,true);
        if(resVo==null){
            return R.Error(EnumRCode.NoFindUsr);
        }
        return R.Success(resVo);
    }

    /**
     * 获取用户的管理部门
     */
    @GetMapping("/userMgr")
    public R<List<GrpIDResVo>> userMgr(@RequestParam @NotNull String usrid) {
        return R.Success(sysGrpService.userMgrList(usrid));
    }

    /**
     * 获取部门下的用户
     */
    @GetMapping("/getActiveUserList")
    public R<List<NamesResVo>> getActiveUserList(@RequestParam @NotNull String grpid) {
        return R.Success(sysUserService.getActiveUserList(grpid));
    }

    /**
     * 修改用户信息
     */
    @PostMapping("/updateUser")
    public R<CmdResVo> updateUser(@RequestBody @Validated UserEditReqVo userEditReqVo) {
        String currUsrID=SpringContextUtil.getCurrUsrID();
        return R.Success(sysUserService.updateUser(currUsrID,userEditReqVo));
    }

    /**
     * 删除用户信息
     */
    @PostMapping("/deleteUser")
    public R<CmdResVo> deleteUser(@RequestBody @Validated UserIDReqVo userIDReqVo) {
        String currUsrID=SpringContextUtil.getCurrUsrID();
        return R.Success(sysUserService.deleteUser(currUsrID,userIDReqVo));
    }

    /**
     * 添加用户信息
     */
    @PostMapping("/insertUser")
    public R<CmdResVo> deleteUser(@RequestBody @Validated UserEditReqVo userEditReqVo) {
        String currUsrID=SpringContextUtil.getCurrUsrID();
        return R.Success(sysUserService.insertUser(currUsrID,userEditReqVo));
    }

    /**
     * 重置用户密码
     */
    @PostMapping("/resetPassword")
    public R<CmdResVo> resetPassword(@RequestBody @Validated UserReqVo userReqVo) {
        String currUsrID=SpringContextUtil.getCurrUsrID();
        return R.Success(sysUserService.resetPassword(currUsrID,userReqVo));
    }


    /**
     * 修改用户密码
     */
    @PostMapping("/updatePwd")
    public R<CmdResVo> updatePwd(@RequestBody @Validated UserUpdatePwdVo userUpdatePwdVo) {
        String currUsrID=SpringContextUtil.getCurrUsrID();
        return R.Success(sysUserService.updatePwd(currUsrID,userUpdatePwdVo));
    }

    /**
     * 更新用户状态
     */
    @PostMapping("/updateStatus")
    public R<CmdResVo> updateStatus(@RequestBody @Validated UserReqVo userReqVo) {
        String currUsrID=SpringContextUtil.getCurrUsrID();
        return R.Success(sysUserService.updateStatus(currUsrID,userReqVo));
    }

    /**
     * 更新组状态
     */
    @PostMapping("/updateGrpStatus")
    public R<CmdResVo> updateGrpStatus(@RequestBody @Validated GrpReqVo grpReqVo) {
        String currUsrID=SpringContextUtil.getCurrUsrID();
        return R.Success(sysGrpService.updateGrpStatus(currUsrID,grpReqVo));
    }

    /**
     * 删除组信息
     */
    @PostMapping("/deleteGrp")
    public R<CmdResVo> deleteGrp(@RequestBody @Validated GrpReqVo grpReqVo) {
        String currUsrID=SpringContextUtil.getCurrUsrID();
        return R.Success(sysGrpService.deleteGrp(currUsrID,grpReqVo));
    }

    /**
     * 修改组信息
     */
    @PostMapping("/updateGrp")
    public R<CmdResVo> updateGrp(@RequestBody @Validated GrpEditReqVo grpEditReqVo) {
        String currUsrID=SpringContextUtil.getCurrUsrID();
        return R.Success(sysGrpService.updateGrp(currUsrID,grpEditReqVo));
    }

    /**
     * 添加组信息
     */
    @PostMapping("/insertGrp")
    public R<CmdResVo> insertGrp(@RequestBody @Validated GrpEditReqVo grpEditReqVo) {
        String currUsrID=SpringContextUtil.getCurrUsrID();
        return R.Success(sysGrpService.insertGrp(currUsrID,grpEditReqVo));
    }

    /**
     * 取全部权限Tree
     */
    @GetMapping("/permissionTree")
    public R<List<PermissionResVo>> permissionTree() {
        return R.Success(sysPermissionService.permissionTree());
    }

    /**
     * 取有效权限Tree
     */
    @GetMapping("/activePermissionTree")
    public R<List<PermissionResVo>> activePermissionTree() {
        return R.Success(sysPermissionService.activePermissionTree());
    }

    /**
     * 取有效权限List
     */
    @GetMapping("/activePermissionList")
    public R<List<NamesResVo>> activePermissionList(@RequestParam @NotNull String parentid) {
        return R.Success(sysPermissionService.activePermissionList(parentid));
    }

    /**
     * 更新权限
     */
    @PostMapping("/updatePermission")
    public R<CmdResVo> updatePermission(@RequestBody @Validated PermEditReqVo permEditReqVo) {
        return R.Success(sysPermissionService.updatePermission(permEditReqVo));
    }

    /**
     *  添加权限
     */
    @PostMapping("/insertPermission")
    public R<CmdResVo> insertPermission(@RequestBody @Validated PermEditReqVo permEditReqVo) {
        return R.Success(sysPermissionService.insertPermission(permEditReqVo));
    }

    /**
     *  删除权限
     */
    @PostMapping("/deletePermission")
    public R<CmdResVo> deletePermission(@RequestBody @Validated PermEditReqVo permEditReqVo) {
        return R.Success(sysPermissionService.deletePermission(permEditReqVo.getId()));
    }

    /**
     *  更新权限状态
     */
    @PostMapping("/updatePermissionStatus")
    public R<CmdResVo> updatePermissionStatus(@RequestBody @Validated PermStatusReqVo permStatusReqVo) {
        return R.Success(sysPermissionService.updatePermissionStatus(
                permStatusReqVo.getPermissionid(),
                permStatusReqVo.getStatus())
        );
    }

    /**
     * 取角色信息LIST
     */
    @GetMapping("/roleList")
    public R<PageData<RoleInfoResVo>> roleList(@Validated RoleInfoReqVo roleInfoReqVo) {
        return R.SuccessPage(sysRoleService.roleList(roleInfoReqVo));
    }

    /**
     * 更新角色状态
     */
    @PostMapping("/updateRoleStatus")
    public R<CmdResVo> updateRoleStatus(@RequestBody @Validated RoleReqVo roleReqVo) {
        return R.Success(sysRoleService.updateStatus(roleReqVo));
    }

    /**
     * 修改角色信息
     */
    @PostMapping("/updateRole")
    public R<CmdResVo> updateRole(@RequestBody @Validated RoleEditReqVo roleEditReqVo) {
        return R.Success(sysRoleService.updateRole(roleEditReqVo));
    }

    /**
     * 添加角色信息
     */
    @PostMapping("/insertRole")
    public R<CmdResVo> insertRole(@RequestBody @Validated RoleEditReqVo roleEditReqVo) {
        return R.Success(sysRoleService.insertRole(roleEditReqVo));
    }

    /**
     * 删除角色
     */
    @PostMapping("/deleteRole")
    public R<CmdResVo> deleteRole(@RequestBody @Validated RoleReqVo roleReqVo) {
        return R.Success(sysRoleService.deleteRole(roleReqVo.getRoleid()));
    }

    /**
     * 取角色对应权限LIST
     */
    @GetMapping("/rolePermission")
    public R<List<PermissionIdResVo>> rolePermission(@RequestParam @NotNull String roleid) {
        return R.Success(sysRoleService.rolePermission(roleid));
    }

    /**
     * 取字典值List
     */
    @GetMapping("/getNamesValues")
    public R<List<NamesResVo>> getNamesValues(@RequestParam @NotNull String tid,String pid) {
        return R.Success(sysNamesService.getNamesValues(tid,pid));
    }

    /**
     * 取字典整型值List
     */
    @GetMapping("/getNamesNumberValues")
    public R<List<NamesNumberResVo>> getNamesNumberValues(@RequestParam @NotNull String tid,String pid) {
        return R.Success(sysNamesService.getNamesNumberValues(tid,pid));
    }

    /**
     * 取字典值List
     */
    @GetMapping("/namesList")
    public R<PageData<NamesInfoResVo>> namesList(@Validated NamesInfoReqVo namesInfoReqVo) {
        return R.SuccessPage(sysNamesService.namesList(namesInfoReqVo));
    }

    /**
     * 取字典节点Tree
     */
    @GetMapping("/getNamesCascading")
    public R<List<NamesTreeResVo>> getNamesCascading(@RequestParam @NotNull String tid) {
        return R.Success(sysNamesService.namesTidTree(tid));
    }

    /**
     * 取字典分类按VALUE
     */
    @GetMapping("/namesTypeSelect")
    public R<List<NamesResVo>> namesTypeSelect() {
        return R.Success(sysNamesService.namesTypeSelect());
    }

    /**
     * 取字典分类按ID
     */
    @GetMapping("/namesType")
    public R<List<NamesTypeResVo>> namesType() {
        return R.Success(sysNamesService.namesType());
    }

    /**
     * 更新字典表
     */
    @PostMapping("/updateNames")
    public R<CmdResVo> updateNames(@RequestBody @Validated NamesEditReqVo namesEditReqVo) {
        return R.Success(sysNamesService.updateNames(namesEditReqVo));
    }

    /**
     * 修改字典状态
     */
    @PostMapping("/updateNameStatus")
    public R<CmdResVo> updateNameStatus(@RequestBody @Validated UpTiDStatusReqVo upTiDStatusReqVo) {
        return R.Success(sysNamesService.updateStatus(upTiDStatusReqVo));
    }

    /**
     * 添加字典表
     */
    @PostMapping("/insertNames")
    public R<CmdResVo> insertNames(@RequestBody @Validated NamesEditReqVo namesEditReqVo) {
        return R.Success(sysNamesService.insertNames(namesEditReqVo));
    }

    /**
     * 删除字典表
     */
    @PostMapping("/deleteNames")
    public R<CmdResVo> deleteNames(@RequestBody @Validated NamesEditReqVo namesEditReqVo) {
        return R.Success(sysNamesService.deleteNames(namesEditReqVo.getTid(),namesEditReqVo.getId()));
    }

    /**
     * 查询审计日志LIST
     */
    @GetMapping("/auditLog")
    public R<PageData<AudLogResVo>> auditLog(@Validated AudLogReqVo audLogReqVo) {
        return R.SuccessPage(sysAuditLogService.auditLog(audLogReqVo));
    }

    /**
     * 取标签List
     */
    @GetMapping("/tagsList")
    public R<PageData<TagsInfoResVo>> tagsList(@Validated TagsInfoReqVo tagsInfoReqVo) {
        return R.SuccessPage(sysTagsService.tagsList(tagsInfoReqVo));
    }

    /**
     * 取标签类别按Value
     */
    @GetMapping("/tagsTypeSelect")
    public R<List<TagsResVo>> tagsTypeSelect() {
        return R.Success(sysTagsService.tagsTypeSelect());
    }

    /**
     * 取标签类别按Name
     */
    @GetMapping("/tagsType")
    public R<List<TagsTypeResVo>> tagsType() {
        return R.Success(sysTagsService.tagsType());
    }

    /**
     * 取标签Tree,hasSys=1系统标签也显示，查询时使用
     */
    @GetMapping("/tagsTree")
    public R<List<TagsTreeResVo>> tagsTree(String hasSys) {
        return R.Success(sysTagsService.tagsTree(hasSys));
    }

    /**
     * 取标签Value.List
     */
    @GetMapping("/tagsSelect")
    public R<List<TagsResVo>> tagsSelect() {
        return R.Success(sysTagsService.tagsSelect());
    }

    /**
     * 修改客户标签
     */
    @PostMapping("/updateTag")
    public R<CmdResVo> updateTag(@RequestBody @Validated TagsEditReqVo tagsEditReqVo) {
        return R.Success(sysTagsService.updateTag(tagsEditReqVo));
    }

    /**
     * 修改标签状态
     */
    @PostMapping("/updateTagStatus")
    public R<CmdResVo> updateTagStatus(@RequestBody @Validated UpTiDStatusReqVo upTiDStatusReqVo) {
        return R.Success(sysTagsService.updateTagStatus(upTiDStatusReqVo));
    }

    /**
     * 添加客户标签
     */
    @PostMapping("/insertTag")
    public R<CmdResVo> insertTag(@RequestBody @Validated TagsEditReqVo tagsEditReqVo) {
        return R.Success(sysTagsService.insertTag(tagsEditReqVo));
    }

    /**
     * 删除客户标签
     */
    @PostMapping("/deleteTag")
    public R<CmdResVo> deleteTag(@RequestBody @Validated TagsDelVo tagsDelVo) {
        return R.Success(sysTagsService.deleteTag(tagsDelVo.getId()));
    }

    /**
     * 清除缓存
     */
    @PostMapping("/delRedis")
    public R<?> delRedis(@RequestBody @Validated RedisKeyReqVo redisKeyReqVo){
        log.info("delRedis:{}",redisKeyReqVo.getKey());
        dataService.del(redisKeyReqVo.getKey());
        return R.Success();
    }

    /**
     * 设置缓存值
     */
    @PostMapping("/setRedis")
    public R<?> setRedis(@RequestBody @Validated RedisSetReqVo reqVo){
        log.info("setRedis:{}",reqVo);
        boolean bret=true;
        if(JPubUtils.IsEqual(reqVo.getKey(),"setusrtoken")){
            String usrid=JwtToken.getUser(reqVo.getValue());
            if(JPubUtils.IsEmpty(usrid)){
                return R.Error(EnumRCode.ArgErr);
            }
            SysUser user=sysUserService.getById(usrid);
            JSysUser jSysUser=new JSysUser();
            BeanUtils.copyProperties(user,jSysUser);
            bret=dataService.set(JPubUtils.getTokenKey(reqVo.getValue()),jSysUser,reqVo.getSpan());
        }
        else{
            bret=dataService.set(reqVo.getKey(),reqVo.getValue(),reqVo.getSpan());
        }
        if(bret) return R.Success();
        else return R.Error(EnumRCode.SetRdErr);
    }

    /**
     * 取缓存值
     */
    @GetMapping("/getRedis")
    public R<?> getRedis(@RequestParam @NotNull String key){
        Object o=dataService.get(key);
        return R.Success(o);
    }

    /**
     * 取TOKEN
     */
    @PostMapping("/createToken")
    public R<CmdResVo> createToken(@RequestBody @Validated TokenReqVo reqVo){
        log.info("createToken:{}",reqVo);

        String token = JwtToken.build(
                JPubUtils.jwt_Secret,
                reqVo.getSpan(),
                reqVo.getUsrid()
        );

        log.info("createToken Ok:{}",token);

        return R.Success(new CmdResVo(1,token));
    }

    /**
     * 取我的组信息
     */
    @GetMapping("/getMyGrpInfo")
    public R<GrpInfoVo> getMyGrpInfo() {
        String usrid=SpringContextUtil.getCurrUsrID();
        return R.Success(sysGrpService.getMyGrpInfo(usrid));
    }
}
